package com.imooc.controller;

import com.imooc.dto.OrderDTO;
import com.imooc.enums.OrderStatusEnum;
import com.imooc.enums.ResultEnum;
import com.imooc.exception.SellException;
import com.imooc.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

@Controller
@RequestMapping("/seller/order")
@Slf4j
public class SellerOrderController{

    @Autowired
    private OrderService orderService;
    // 1. 接受前端传来的分页参数

    /**
     * 显示所有用户的订单数据
     * @param page
     * @param size
     * @param map
     * @return
     */
     @GetMapping("/list")
     public ModelAndView list(@RequestParam(value="page" ,  defaultValue = "1") Integer page,
                              @RequestParam(value = "size", defaultValue = "3") Integer size,
                              Map<String, Object> map) {

         // 2. 根据 分页查询 所有用户信息，调用service 查询方法
         PageRequest request = new PageRequest(page-1, size);
         Page<OrderDTO> orderDTOPage = orderService.findList(request);
         // 3. 将查询结果塞到 modelandview 前端进行渲染
         map.put("orderDTOPage", orderDTOPage);
         map.put("currentPage", page);
         map.put("size", size);
        return new ModelAndView("order/list", map);


     }

     // 取消订单

    /**1. 先查询订单
     * - 存在？ - 取消，返回成功界面
     * - 不存在？ - 报错， 返回错误界面
     * @param orderId
     * @param map
     * @return
     */
    @GetMapping("/cancel")
    public ModelAndView cancel(@RequestParam(value = "orderId") String orderId,
                               Map<String, Object> map){
        try{
            OrderDTO orderDTO = orderService.findOne(orderId);
            orderService.cancel(orderDTO);

        }catch (SellException e){
            log.error("【订单取消发生错误】{}" ,e) ; // + ResultEnum.ORDER_CANCEL_ERROR
            map.put("msg", e.getMessage());  //ResultEnum.ORDER_CANCEL_ERROR.getMessage()
            map.put("url", "/sell/seller/order/list");
            return new ModelAndView("/common/error", map);
        }

        map.put("msg", ResultEnum.ORDER_CANCEL_SUCCESS.getMessage());
        map.put("url", "/sell/seller/order/list");
        // return new ModelAndView("common/success", map);
        return new ModelAndView("common/success", map);
    }

    /** 1. 根据前端传来的id号 查询数据库中订单信息
     *       成功： 跳转详情
     *       失败： 跳转错误页面
     *
     * @param orderId
     * @param map
     * @return
     */
    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam(value = "orderId") String orderId,
                               Map<String, Object> map){
        OrderDTO orderDTO = new OrderDTO();
        try {
            // 查询不到就 到错误页--回到当前页
            orderDTO = orderService.findOne(orderId);
        }catch (SellException e){
            log.error(e.getMessage());
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/order/list");
            return new ModelAndView( "common/error", map);
        }
        // 成功就跳到 成功页 -- 返回当前页面
        map.put("orderDTO", orderDTO);
        map.put("msg",ResultEnum.SUCCESS.getMessage());         //show msg
        map.put("url", "/sell/seller/order/list");              // direct url
        return new ModelAndView("order/detail", map);

    }

    /** 1. 先查询
     *  2.  完结订单
     *       成功 -- 跳转成功页面
     *       失败 -- 跳转失败页面
     *
     * @param orderId
     * @param map
     * @return
     */
    @GetMapping("/finish")
    public ModelAndView finish(@RequestParam(value = "orderId") String orderId,
                               Map<String, Object> map){
        OrderDTO orderDTO = new OrderDTO();
        try {
            orderDTO = orderService.findOne(orderId);
            orderDTO = orderService.finish(orderDTO);
        }catch (SellException e){
            // 失败
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/order/list");
            return new ModelAndView("common/error", map);
        }
        map.put("msg", ResultEnum.SUCCESS);
        map.put("url", "/sell/seller/order/list");
        return new ModelAndView("common/success", map);

    }}
